package demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-28
 * Time: 18:18
 */
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker1) {
                    try {
                        locker1.wait();
                        System.out.print("A");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                synchronized (locker2) {
                    locker2.notify();
                }
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker2) {
                    try {
                        locker2.wait();
                        System.out.print("B");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                synchronized (locker3) {
                    locker3.notify();
                }

            }
        });

        Thread t3 = new Thread(()->{
            for (int i = 0; i < 10; i++) {

                synchronized (locker3) {
                    try {
                        locker3.wait();
                        System.out.println("C");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                synchronized (locker1) {
                    locker1.notify();
                }
            }
        });


        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        synchronized (locker1){
            locker1.notify();
        }
    }
}
